//
//  HelpScrollView.m
//  51-Tour
//
//  Created by xiang ying on 14-3-25.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "HelpScrollView.h"

@implementation HelpScrollView

- (id)initWithLanguage:(BOOL)btnshow{
    CGFloat height = [[UIScreen mainScreen] bounds].size.height;
    self = [super initWithFrame:CGRectMake(0, 0, 320, height)];
    if (self) {
        num = 4;
        for (int i = 0; i<num; i++) {
            UIImageView *help = [[UIImageView alloc] initWithFrame:CGRectMake(i*320, 0, 320, height)];
            help.image = [UIImage imageNamed:[NSString stringWithFormat:@"help-guide-page%d",i+1]];
            [self addSubview:help];
        }
        self.contentSize = CGSizeMake(320*num+80, height);
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        if (btnshow) {
            zhBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [zhBtn setImage:[UIImage imageNamed:@"radio-box-selected_17"] forState:UIControlStateSelected];
            [zhBtn setImage:[UIImage imageNamed:@"radio-box_17"] forState:UIControlStateNormal];
            [zhBtn setFrame:CGRectMake(320*(num-1)+50, height-120, 100, 48)];
            [zhBtn addTarget:self action:@selector(selectLanguage:) forControlEvents:UIControlEventTouchUpInside];
            [zhBtn setTitle:@"  中文" forState:UIControlStateNormal];
            [zhBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            zhBtn.titleLabel.font = [UIFont systemFontOfSize:13];
            zhBtn.selected = YES;
            [self addSubview:zhBtn];
            
            enBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [enBtn setImage:[UIImage imageNamed:@"radio-box-selected_17"] forState:UIControlStateSelected];
            [enBtn setImage:[UIImage imageNamed:@"radio-box_17"] forState:UIControlStateNormal];
            [enBtn setFrame:CGRectMake(320*num-100-50, height-120, 100, 48)];
            [enBtn addTarget:self action:@selector(selectLanguage:) forControlEvents:UIControlEventTouchUpInside];
            [enBtn setTitle:@"  English" forState:UIControlStateNormal];
            [enBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            enBtn.titleLabel.font = [UIFont systemFontOfSize:13];
            [self addSubview:enBtn];
        }
        
        self.delegate = self;
        
    }
    return self;
}

-(void)selectLanguage:(UIButton*)sender{
    if (!sender.selected) {
        sender.selected = YES;
        if (sender == zhBtn) {
            enBtn.selected = NO;
        }else {
            zhBtn.selected = NO;
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.contentOffset.x>(num-1)*320+50) {
        [UIView animateWithDuration:0.5 animations:^{
            self.center = CGPointMake(-160, self.center.y);
        } completion:^(BOOL finished) {
            if (zhBtn) {
                [[DataCenter instance] setLanguangeType:zhBtn.selected?zh_cn:en_us];
            }
            [self removeFromSuperview];
        }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x>(num-1)*320+50) {
        [UIView animateWithDuration:0.5 animations:^{
            self.center = CGPointMake(-160, self.center.y);
        } completion:^(BOOL finished) {
            if (zhBtn) {
                [[DataCenter instance] setLanguangeType:zhBtn.selected?zh_cn:en_us];
            }
            [self removeFromSuperview];
        }];
    }
}
@end
